Contoh penggunaan templat Bantuan:Templat

Jika anda ingin mencuba templat-templat di bawah, sila gunakan templat kotak pasir

Templat dengan tiada parameter

Untuk melihat contoh templat yang paling mudah, perhatikan Template:Tc. Templat ini membantu mengalihkan huruf di "dalam" ke halaman lain. Contohnya, menyunting frasa berikut ke mana-mana halaman akan menghasilkan hasil seperti di bawah:

Menggunakan templat {{tc}}
PeneranganPerkataan yang dimasukkanHasilnya
Hanya {{Tc}}{{tc}}{{tlc|...}}
Hanya satu penggunaan {{tc}} dalam teks{{tc}} 2009, with Barack Obama settling into the White House, we find the beginning of...{{tlc|...}} 2009, with Barack Obama settling into the White House, we find the beginning of...
Penggunaan {{tc}} yang banyak dalam teks{{tc}} 2009, with Barack Obama settl{{tc}}g {{tc}}to the White House, we f{{tc}}d the beg{{tc}}n{{tc}}g of...{{tlc|...}} 2009, with Barack Obama settl{{tlc|...}}g {{tlc|...}}to the White House, we f{{tlc|...}}d the beg{{tlc|...}}n{{tlc|...}}g of...

Anda akan perasan bahawa templat tersebut hanya mengandungi huruf "dalam" apabila dikosongkan, walaupun ada teks penjelasan pada halaman templat ("Templat ini digunakan ..." klik pada pautan templat di atas untuk melihat). Jika anda menyunting halaman templat, (klik pautan ini), anda akan melihat semuanya kecuali huruf-huruf di"dalam" yang disertakan dalam 'noinclude', yang akan dibincangkan kemudian.

Templat parameter tiada nama

Transclusion with parameters is only slightly more complicated. For a quick example, the famous dead man's hand can be reproduced by the following templates, using unnamed parameters:

making the Dead Man's Hand
DescriptionText EnteredResult after Processing
using three templates (Template:Clubs, Template:Diamonds, and Template:Spades){{ clubs |A}} {{ clubs |8}} {{ spades |A}} {{ spades |8}} {{ diamonds |9}}A♣ 8♣ A♠ 8♠ 9♦
Using (Template:BridgeHandInline); not quite as pretty, but just one template, with four parameters.{{ BridgeHandInline |A8||9|A8}}♠ A8 ♥  ♦ 9 ♣ A8
Using (Template:BridgeHandInline), but with a missing pipe separator{{ BridgeHandInline |A8|9|A8}}♠ A8 ♥ 9 ♦ A8 ♣ {{{4}}}

In the second example, the position of the parameter in the template tag determines which suit it belongs to. Thus, the first A8 (first slot) goes to spades, the empty position (second slot) goes to hearts, the 9 (third slot) goes to diamonds, the second A8 (fourth slot) goes to clubs. Note that the empty parameter position is counted; if the extra pipe is forgotten, as in the third example, the result is that the cards end up in the wrong positions, and a {{{4}}} appears, meaning that the template was expecting a value for the fourth positional parameter but none was provided. See the section on parameters and default values, below.

Templat dengan parameter bernama

Using named parameters is similar to using unnamed parameters. For example, Template:Payoff matrix (which is used in some game theory pages) gives a simple 2-by-2 grid with adjustable values, using the names UL, UR, DL, and DR to refer to Up Left, Up Right, Down Left and Down Right, and a Name parameter to add a name to the bottom of the matrix.

DescriptionCodeResult
Payoff matrix{{payoff matrix | UL = 5 | UR = 7 | DL = 2 | DR = 9 | Name = Example usage }}Templat:Payoff matrix
Payoff matrix showing a default value for a missing parameter, and different ordering for the others{{payoff matrix | DR = 9 | Name = Example usage | DL = 2 | UR = 7 }}Templat:Payoff matrix

In the second case, the order of the parameters has been altered without changing the result - one of the advantages that named parameters have over unnamed parameters - and one of the values (UL) has been removed entirely, allowing the template to insert the default value of "0, 0".

Parameter names are case sensitive, even in the first character. for example, using "dr = 9","Dr = 9" or "dR = 9" instead of "DR = 9" will cause the Down Right cell to display the default "0, 0". The alternate case versions are treated as different parameters and ignored by the template.

Penggantian

Laman utama: Help:Penggantian

Generally, templates are processed into readable text when a user browses a page. This is the normal intention of a template: since the template is reevaluated each time it's used, changes to the template can propagate across many pages quickly and effortlessly. Occasionally, however, this is not appropriate. For instance, a user may want a template which returns a date or time that remains fixed, rather than changing each time the page is browsed. For those cases there is substitution, which is done by adding subst: before the template name in the template tag. substitution does exactly what its name suggests: rather than processing the template each time the page is browsed, substitution processes the template at the time the edit is saved, and replaces the template tag with the processed results.

For example, the template {{tc}} (as shown above) adds the letters "in" wherever it is placed. Thus, the wikitext bra{{tc}} will produce the word bra{{tlc|...}}. The wikitext will always remain as bra{{tc}}, however, so if {{tc}} is changed so that it adds the letters "wn", the wikitext will produce brawn instead of brain. Substituting like so - bra{{subst:tc}} - will permanently change the wikitext to read brain, and later changes to the template {{tc}} will have no effect on the substituted text.

In general, substitution processes one level, so if the template in question contains other template tags, those template tags will be substituted in, rather than the processed results of those tags. This is a sometimes useful trick when debugging complex templates. For information on multi-level substitution, see Help:Substitution#Multilevel substitution. Articles where templates were substituted rather than transcluded are listed on WikiProject Check Wikipedia#Template programming element.

Lain-lain maklumat penggunaan

  • To quote a template, creating a link to the template rather than actually invoking the template, use the "{{tl}}" template (the template link template). For example, using the text "{{tl|tc}}" creates a link to Template:Tc without actually doing what the template normally does, like so: {{tc}}.
  • a list of all pages that a particular template is transcluded onto can be found by clicking the 'What links here' link on the main template page.
  • a list of all the templates that a particular page transcludes can be found by clicking the Edit link on the page normally and scrolling down past the edit window. The list will include the entire template "tree", that is, any sub-templates called by other templates to produce the preview will also be listed. To find the actual templates used in the article page itself, you will need to search in the edit window text for template invocations (names included within the "{{...}}" wiki-template invocations).
    • A list of templates used in a particular section can be found by clicking the Edit link for that section and then clicking Preview
    • Likewise, a list of templates used in an old version of a page can be found by choosing the version from the page history, clicking the Edit link, and clicking Preview. Note that this only works to one level: the sub-templates listed will reflect the current state of the templates transcluded on the old page. There is no way to examine which sub-templates were transcluded at the time the old page was current.
  • Templates may seem small compared to other pages on Wikimedia, but editing them can consume a tremendously disproportionate amount of system resources, particularly when they are transcluded on to large numbers of pages. When a change is made to a template, the MediaWiki software automatically places every page in which the template is transcluded onto the job queue to update the template links and ensure that any new page views will reflect the change. For very popular templates, it is better to create a copy in userspace or in a sandbox, make and test any needed changes there, present it on the template talk page to make sure there are no objections, and then introduce it as a single edit. See Wikipedia:Template test cases for a systematic way of doing so.
  • In rare circumstances, templates may be so complex and invoke so many sub-templates that they impose a significant load on the parser software. This load can be checked by examining the generated HTML for a page and looking for the "NewPP limit report" comments, which show how relatively "expensive" the article page is.